
package com.huantai.common.util;

import java.util.UUID;

/**
 * UUID Util
 * 
 * @author Ma Chao
 * @version 2018年5月2日 下午3:13:22
 */
public final class UUIDUtil {
    /** 不允许使用对象来调用工具类的方法 */
    private UUIDUtil() {}

    /** 基础字符 */
    private static final String BASE_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789";
    /** 基础数字字符 */
    private static final String BASE_NUM_CHARS = "0123456789";

    /**
     * 获取随机字符串
     * 
     * @return 随机字符串
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取长度为length的随机字符串
     * 
     * @param length
     * @return
     */
    public static String getRandomString(int length) {
        StringBuilder result = new StringBuilder();
        int charCount = BASE_CHARS.length();
        for (int i = 0; i < length; i++) {
            int index = RandomUtil.nextInt(charCount);
            result.append(BASE_CHARS.charAt(index));
        }
        return result.toString();
    }

    /**
     * 获取长度为length的随机数字字符串
     * 
     * @param length
     * @return
     */
    public static String getRandomNumString(int length) {
        StringBuilder result = new StringBuilder();
        int charCount = BASE_NUM_CHARS.length();
        for (int i = 0; i < length; i++) {
            int index = RandomUtil.nextInt(charCount);
            result.append(BASE_NUM_CHARS.charAt(index));
        }
        return result.toString();
    }
}
